<?php
class UserService
{
	public function setSession($user,$life = 0)
	{
		fSetCookie('auth', authcode("$user[user_pwd]\t$user[id]", 'ENCODE'), $life);
	}

	public function clearSession()
	{
		fSetCookie('auth','');
	}

	public function getEmailExists($email)
	{
		if(intval(DB::resultFirst('SELECT COUNT(id) FROM '.DB::table('user')." WHERE email = '$email'")) > 0)
			return true;
		else
			return false;
	}

	public function getUserNameExists($user_name)
	{
		if(intval(DB::resultFirst('SELECT COUNT(id) FROM '.DB::table('user')." WHERE user_name = '$user_name'")) > 0)
			return true;
		else
			return false;
	}

	public function getUserById($uid)
	{
		static $user = array();

		if(!isset($user[$uid]))
		{
			$user[$uid] = DB::fetchFirst("SELECT * FROM ".DB::table('user')." WHERE id='$uid'");
		}

		return $user[$uid];
	}

	public function getUserByEmailPwd($email,$pwd)
	{
		return DB::fetchFirst("SELECT * FROM ".DB::table('user')." WHERE email='$email' AND user_pwd = '$pwd'");
	}

	public function getUserByNamePwd($user_name,$pwd)
	{
		return DB::fetchFirst("SELECT * FROM ".DB::table('user')." WHERE user_name='$user_name' AND user_pwd = '$pwd'");
	}


	public function getCustomSite()
	{
		static $custom_sites = NULL;
		if($custom_sites === NULL)
		{
			global $_FANWE;
			$custom_sites = DB::resultFirst('SELECT GROUP_CONCAT(site_id) FROM '.DB::table('user_site').' WHERE user_id = '.$_FANWE['uid'].' GROUP BY user_id');

		}

		return $custom_sites;
	}

	public function updateCustomSite($site_ids)
	{
		global $_FANWE;
		$site_ids = explode(',',$site_ids);
		DB::query('DELETE FROM '.DB::table('user_site').' WHERE user_id = '.$_FANWE['uid']);
		foreach($site_ids as $site_id)
		{
			DB::query('INSERT INTO '.DB::table('user_site').'(user_id,site_id) VALUES('.$_FANWE['uid'].','.$site_id.')');
		}
	}

	public function getUserUcid($uid)
	{
		global $_FANWE;
		if(intval($_FANWE['user']['id']) != $uid)
			$ucenter_id = DB::resultFirst('SELECT ucenter_id FROM '.DB::table('user').' WHERE id = '.$uid);
		else
			$ucenter_id = $_FANWE['user']['ucenter_id'];

		return $ucenter_id;
	}

	public function getUserScore($uid,$ucenter_id = 0)
	{
		global $_FANWE;

		if(intval($_FANWE['user']['id']) != $uid)
		{
			$user = DB::fetchFirst('SELECT score,ucenter_id FROM '.DB::table('user').' WHERE id = '.$uid);
			$ucenter_id = $user['ucenter_id'];
		}
		else
		{
			if($ucenter_id == 0)
				$ucenter_id = $_FANWE['user']['ucenter_id'];
		}

		$code = $_FANWE['setting']['integrate_code'];
		if (empty($code))
			$code = 'fanwe';

		if($code != 'fanwe')
		{
			$users = &init_users();
			return $users->get_credit($ucenter_id);
		}
		else
		{
			if(intval($_FANWE['user']['id']) != $uid)
				return $user['score'];
			else
				return $_FANWE['user']['score'];
		}
	}

	public function updateUserScore($user_id,$model,$action,$msg='',$rec_id = 0,$score = 0,$is_log = true)
	{
		global $_FANWE;
		$handle = strtolower($model."_".$action."_score");
		if($handle != "jifen_exchange_score" && $score == 0)
		{
			switch($handle)
			{
				case "goods_comment_score":
				case "site_comment_score":
					$score = intval($_FANWE['setting']['user_comment_score']);
				break;

				case "site_click_score":
				case "goods_click_score":
					$score = intval($_FANWE['setting']['user_click_score']);
				break;

				default:
					$score = intval($_FANWE['setting'][$handle]);
				break;
			}
		}

		$code = $_FANWE['setting']['integrate_code'];
		if (empty($code))
			$code = 'fanwe';

		if($code != 'fanwe')
			$users = &init_users();

		if(abs($score) > 0)
		{
			if($rec_id == 0)
				$rec_id = $user_id;

			switch($handle)
			{
				case "user_login_score":
				{
					$todayTime = mktime(0, 0, 0, date("m")  , date("d"), date("Y")) - date('Z');
					$where .= " AND last_time < $todayTime";
				}
				break;
			}

			$is_update = true;

			if($code != 'fanwe')
			{
				$ucenter_id = $this->getUserUcid($user_id);
				$is_update = $users->update_credit($ucenter_id,$score);
			}
			else
				$is_update = DB::query("UPDATE ".DB::table('user')." SET score = score + ".intval($score)." WHERE id = $user_id".$where, 'UNBUFFERED');

			if($is_update !== false)
			{
				if($is_log)
				{
					$log['user_id'] = $user_id;
					$log['score'] = $score;
					$log['create_time'] = TIME_UTC;
					if(empty($msg))
						$log['memo'] = lang('user',strtolower($handle));
					else
						$log['memo'] = $msg;

					$log['rec_id'] = $rec_id;
					$log['rec_module'] = $model;
					$log['rec_action'] = $action;

					DB::insert('user_score_log',$log);
				}
			}
			else
				return false;
		}

		return true;
	}

	public function referralHandle($uid,$pid,$user_name)
	{
		global $_FANWE;

		if($pid > 0)
		{
			$referrals['user_id'] = $uid;
			$referrals['parent_id'] = $pid;
			$referrals['score'] = intval($_FANWE['setting']['user_referral_score']);
			$referrals['is_pay'] = 1;
			$referrals['create_time'] = TIME_UTC;
			$referrals['pay_time'] = TIME_UTC;
			$rec_id = DB::insert('referrals',$referrals,true);

			if($rec_id > 0)
			{
				$msg = sprintf(lang('user','user_referral_score'),$user_name);
				$this->updateUserScore($pid,"User","Referral",$msg,$rec_id);
				fSetCookie('referrals_uid','');
			}
		}
	}

	public function setReferrals()
	{
		global $_FANWE;
		if (!empty($_FANWE['request']['ru']))
		{
			$parent_id = intval($_FANWE['request']['ru']);
			if($parent_id > 0)
				fSetCookie('referrals_uid', authcode($parent_id, 'ENCODE'),0, 1, true);
		}
	}

	public function getReferrals()
	{
		global $_FANWE;

		if($_FANWE['cookie']['referrals_uid'])
		{
			$uid = intval(authcode($_FANWE['cookie']['referrals_uid'], 'DECODE'));
			if ($uid > 0)
			{
				if (DB::resultFirst('SELECT COUNT(id) FROM '.DB::table('user').' WHERE id = '.$uid) > 0)
					return $uid;
				else
				{
					fSetCookie('referrals_uid',0,-1, 1, true);
				}
			}
		}

		return 0;
	}
}
?>